package com.fan.config;

import com.fan.serializer.FieldPermissionBeanSerializerModifier;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * 字段权限Jackson配置
 * 用于注册自定义的序列化器修改器
 */
@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class FieldPermissionJacksonConfig {

    @Autowired
    private FieldPermissionBeanSerializerModifier fieldPermissionBeanSerializerModifier;

    /**
     * 配置ObjectMapper，添加字段权限序列化器修改器
     */
    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // 忽略未知属性
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注册自定义的序列化器修改器
        objectMapper.setSerializerFactory(
            objectMapper.getSerializerFactory()
                .withSerializerModifier(fieldPermissionBeanSerializerModifier)
        );
        
        return objectMapper;
    }
}